package cz.muni.fi.pa036is.servicesimplementation;

import cz.muni.fi.pa036is.utilities.ScoreCalculationService;
import cz.muni.fi.pa036is.daointerface.MatchDAO;
import cz.muni.fi.pa036is.daointerface.PersonDAO;
import cz.muni.fi.pa036is.daointerface.RoundDAO;
import cz.muni.fi.pa036is.daointerface.SeasonDAO;
import cz.muni.fi.pa036is.dto.MatchGridTO;
import cz.muni.fi.pa036is.dto.PersonDetailTO;
import cz.muni.fi.pa036is.dto.SeasonBasicTO;
import cz.muni.fi.pa036is.dto.SeasonDetailTO;
import cz.muni.fi.pa036is.dto.SeasonFormTO;
import cz.muni.fi.pa036is.dto.SeasonGridTO;
import cz.muni.fi.pa036is.dtoconverters.MatchConverter;
import cz.muni.fi.pa036is.dtoconverters.PersonConverter;
import cz.muni.fi.pa036is.dtoconverters.SeasonConverter;
import cz.muni.fi.pa036is.entity.Match;
import cz.muni.fi.pa036is.entity.Person;
import cz.muni.fi.pa036is.entity.Season;
import cz.muni.fi.pa036is.servicesinterface.SeasonService;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Shebik
 */
@Service
public class SeasonServiceImpl implements SeasonService {

    @Autowired
    private SeasonDAO seasonDAO;
    
    @Autowired
    private PersonDAO personDAO;
    
    @Autowired
    private MatchDAO matchDAO;

    @Autowired
    private RoundDAO roundDAO;

    @Autowired
    private ScoreCalculationService scoreCalculationService;

    @Override
    public void addSeason(final SeasonFormTO seasonFormTO) {
								Season season = SeasonConverter.convertToSeason(seasonFormTO);
								seasonDAO.addSeason(season);
								seasonFormTO.setId(season.getId());
    }

    @Override
    public void editSeason(final SeasonFormTO seasonFormTO) {
        seasonDAO.editSeason(SeasonConverter.convertToSeason(seasonFormTO));
    }

    @Override
    public void deleteSeason(final Long id) {
        for (final Match match : matchDAO.getAllMatchesForSeason(id)) {
            matchDAO.deleteMatch(match.getId());
        }
        seasonDAO.deleteSeason(id);
    }

    @Override
    public SeasonFormTO getSeasonForm(final Long id) {
        return SeasonConverter.convertToSeasonFormTO(seasonDAO.findSeason(id));
    }

    @Override
    public SeasonDetailTO getSeasonDetail(final Long id) {
        final SeasonDetailTO seasonDetailTO = SeasonConverter
                .convertToSeasonDeatilTO(seasonDAO.findSeason(id));
        seasonDetailTO.setMatchList(convertToMatchGridTOList(
                matchDAO.getAllMatchesForSeason(id)));
        return seasonDetailTO;
    }

    @Override
    public List<SeasonGridTO> getAllSeasons() {
        return SeasonConverter.convertToSeasonGridTOList(seasonDAO.getAllSeasons());
    }

    public List<SeasonBasicTO> getAllSeasonsForDropDown() {
        return SeasonConverter.convertToSeasonBasicTOList(seasonDAO.getAllSeasons());
    }

    private List<MatchGridTO> convertToMatchGridTOList(final List<Match> matchList) {
        List<MatchGridTO> matchGridTOList = new ArrayList<MatchGridTO>();

        for (final Match match : matchList) {
            final Season season = seasonDAO.findSeason(match.getSeasonId());
            final Person player1 = personDAO.findPerson(match.getPlayer1Id());
            final Person player2 = personDAO.findPerson(match.getPlayer2Id());
            String finalScore = scoreCalculationService.calculateFinalScore(
                    match.getId(), season, roundDAO.getAllRoundsForMatch(match.getId()));
            matchGridTOList.add(MatchConverter.convertToMatchGridTO(
                    match, season, player1, player2, finalScore));
        }

        return matchGridTOList;
    }

				@Override
				public List<SeasonDetailTO> getSeasonsByPerson(PersonDetailTO personDetailTO) {
								
								List<MatchGridTO> matches = new ArrayList<>();
								for (Match m: this.matchDAO.getAllMatchesForPerson(personDetailTO.getId())) {
												
												Season s = this.seasonDAO.findSeason(m.getSeasonId());
												Person p1 = this.personDAO.findPerson(m.getPlayer1Id());
												Person p2 = this.personDAO.findPerson(m.getPlayer2Id());
												
												String finalScore = 
																				this.scoreCalculationService.calculateFinalScore(
																				m.getId(), s, this.roundDAO.getAllRoundsForMatch(m.getId()));
												
												matches.add(MatchConverter.convertToMatchGridTO(m, s, p1, p2, finalScore));
								}
								
								List<SeasonDetailTO> seasons = new ArrayList<>();
								for (Season s: this.seasonDAO.getSeasonsByPerson(PersonConverter.convertToPerson(personDetailTO))) {
												
												SeasonDetailTO season = SeasonConverter.convertToSeasonDeatilTO(s);
												season.setMatchList(matches);
												
												seasons.add(season);
								}
								
								return seasons;
				}

}
